package cn.huanghuai.reggie.controller;

import cn.huanghuai.reggie.common.R;
import cn.huanghuai.reggie.entity.Employee;
import cn.huanghuai.reggie.service.EmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    //接收post请求体中的用户名和密码并封装到employee对象中
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        //1.因为我们后端密码是md5加密过的，所以要将前端发送过来的密码先进行Md5加密再进行比对
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2.根据用户名去数据库中查询
            //编写查询条件
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
            //查询数据
        Employee emp = employeeService.getOne(queryWrapper);

        //3.看数据是否为空
        if(emp == null){
            return R.error("账号或密码错误");
        }

        //4.比对密码
        if(!emp.getPassword().equals(password)){
            return R.error("账号或密码错误");
        }

        //5.查看员工状态
        if (emp.getStatus() == 0){
            return R.error("账号已禁用");
        }

        //6.可以登录，保存用户id的session方便后续使用
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    /**
     * 退出操作，要删除session中员工的id数据
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 添加员工 接收请求体中的json数据
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee){

        //设置初始密码为123456，不能使用明文，要md5加密一下
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

//        //设置创建时间 LocalDateTime.now()为当前计算机时间
//        employee.setCreateTime(LocalDateTime.now());
//        //设置更新时间
//        employee.setUpdateTime(LocalDateTime.now());
//        //设置创建人的id，当前登录用户的id,再session中存有登录用户的id
//        Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(empId);
//        //设置更新人的id
//        employee.setUpdateUser(empId);

        employeeService.save(employee);

        return R.success("新增员工成功");
    }

    /**
     * 员工信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name){
        //分页构造器
        Page pageInfo = new Page(page, pageSize);

        //条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        //执行查询
        employeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 根据id修改员工信息
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee){

//        //修改前要更新 修改时间和修改人
//        employee.setUpdateTime(LocalDateTime.now());
//        Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateUser(empId);

        //调用修改操作    - 当前更新操作是只更行有数据的一部分，数据为null的不会拼接到sql语句
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> gteById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if (employee != null)
            return R.success(employee);
        else
            return R.error("没有查询到对应员工信息");
    }
}
